package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.exception.BusinessException;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired(required = false)
    private EmployeeMapper employeeMapper;


    /**
     * 登录的业务方法
     *
     * @param employee
     * @return
     */
    @Override
    public R<Employee> login(Employee employee) {
        //1.检验用户是否存在
        //调用mapper根据用户名查找用户对象
        Employee login = employeeMapper.login(employee);
        //判断用户对象是否为null,为null说明用户不存在,提示给用户
        if (login == null) {
            throw new BusinessException("用户不存在,请先注册");
        }

        //2.校验密码是否正确
        //对用户输入的明文密码进行加密
        String md5 = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        //在于数据库密文对比
        if (!login.getPassword().equals(md5)) {
            //如果不一样,提示用户去修改
            throw new BusinessException("用户或密码错误,请重新输入");
        }

        //3.校验是否为禁用状态
        if (login.getStatus() == 0) {
            throw new BusinessException("你的账号已被禁用,请联系管理员");
        }

        return R.success(login);
    }

    /**
     * 添加用户
     *
     * @param employee
     * @return
     */
    @Override
    public boolean save(Employee employee) {
        //1.校验账号username唯一
        //根据账号名查询用户
        Employee login = employeeMapper.login(employee);
        //判断login是否存在,如果不为null,表示存在,提示给用户
        if (login != null) {
            throw new BusinessException("用户已存在,请修改");
        }

        //2.完善数据
        //默认密码
        employee.setPassword(DigestUtils.md5DigestAsHex("147258".getBytes()));

        //状态
        employee.setStatus(1);

        //创建时间
        employee.setCreateTime(LocalDateTime.now());

        //修改时间
        employee.setUpdateTime(LocalDateTime.now());

        //调用mapper添加到数据库
        employeeMapper.save(employee);

        return true;
    }

    /**
     * 分页查询员工的数据
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<Employee> findByPage(Integer page, Integer pageSize, String name) {
        //1.设置分页查询第几页和每页多少条
        PageHelper.startPage(page, pageSize);

        //2.执行查询所有数据的sql语句方法
        List<Employee> byName = employeeMapper.findByName(name);

        //3.使用PageInfo封装上面的结果
        PageInfo<Employee> employeePageInfo = new PageInfo<>(byName);

        Page<Employee> objectPage = new Page<>();
        objectPage.setRecords(employeePageInfo.getList());
        objectPage.setTotal(employeePageInfo.getTotal());

        return objectPage;
    }

    /**
     * 修改用户账号禁用启用状态
     *
     * @param employee
     * @return
     */
    @Override
    public boolean update(Employee employee) {
        employee.setUpdateTime(LocalDateTime.now());
        return employeeMapper.update(employee) > 0;
    }

    /**
     * 编辑员工信息(通过id查找)
     *
     * @param id
     * @return
     */
    @Override
    public R<Employee> findById(Long id) {
        Employee byId = employeeMapper.findById(id);
        return R.success(byId);
    }


}
